
package cnhy.salary.controller;

import cnhy.salary.domain.Employee;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import lombok.*;
import com.ruoyi.common.core.page.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import cnhy.salary.domain.PostCfg;
//import cnhy.salary.service.PostCfgService;
import ling.common.*;
import ling.common.domain.*;
import org.springframework.web.multipart.MultipartFile;

/** 岗位工资配置 控制层
 * @author : He-LiangLiang@Outlook.com
 * @date : 2025-5-26
 */
@Api(tags = "岗位工资配置 功能接口")
@RestController
@RequestMapping("/salary/PostCfg")
public class PostCfgController extends EQueryController<PostCfg>{
    

    
    @ApiOperation("详情")
    @PreAuthorize("@ps.any(@pay.postCfg.query)")
    @GetMapping(value = "/{id}")
    public Result<PostCfg> getInfo(@PathVariable("id") Long id)
    {
       return super.getInfo(id);
    }
    
    @ApiOperation("分页列表")
    @PreAuthorize("@ps.any(@pay.postCfg.list)")
    @GetMapping("list")
    public ResultRows<PostCfg> list(PostCfg dto, PageDomain pageRequest) {
        return queryPage(dto,pageRequest);
    }
    
    @ApiOperation("新增")
    @PreAuthorize("@ps.any(@pay.postCfg.add)")
    @Log(title = "岗位工资配置", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@Validated @RequestBody PostCfg entity){
        if(StringUtils.isBlank(entity.getName()))
            return Result.bad("名称不能为空");
       var old=queryHelper.queryable(PostCfg.class).where(p -> p.eq(PostCfg::getName,entity.getName())).firstOrNull();
       if(old!=null)
           return Result.bad("岗位名称重复");
        return super.add(entity);
    }
    
    @ApiOperation("删除")
    @PreAuthorize("@ps.any(@pay.postCfg.remove)")
    @Log(title = "岗位工资配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable("ids") Long[] ids){

        var count=queryHelper.entityQuery(Employee.class)
                .where(p->p.post().in(ids))
                .where(p->p.resignTime().isNull())
                .count();
        if(count>0)
            return Result.bad("岗位使用中无法删除，请先将岗位内人员移到其他岗位再删除");
        return super.remove(Arrays.stream(ids).toArray());
    }
    
    @ApiOperation("编辑")
    @PreAuthorize("@ps.any(@pay.postCfg.edit)")
    @Log(title = "岗位工资配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public  Result edit(@Validated @RequestBody PostCfg entity){
        return super.edit(entity);
    }
    
    @ApiOperation("导出")
    @PreAuthorize("@ps.any(@pay.postCfg.export)")
    @Log(title = "岗位工资配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PostCfg entity, PageDomain pageRequest,HttpServletResponse response)
    {
        super.export(entity,pageRequest,null,response);
    }



//    @PostMapping("/importData")
//    public Result importData(MultipartFile file) throws Exception {
//        var util = new ExcelUtil<>(PostCfg.class);
//        var list = util.importExcel(file.getInputStream());
//
//
//    }
//
//    @PostMapping("/importTemplate")
//    public void importTemplate(HttpServletResponse response)
//    {
//        ExcelUtil<PostCfg> util = new ExcelUtil<>(PostCfg.class);
//        util.importTemplateExcel(response, "岗位导入模板");
//    }
}